<template>
	<view>
		<view class="search-box">
		  <my-search @click="gotoSearch"></my-search>
		</view>
		<view class="scroll-view-container">
			<scroll-view class="scroll-left" :style="{height: wh + 'px'}" scroll-y>
				<view :class="['left-scroll-item',index === active ? 'active' : '']" @click="activeChange(index)"
					v-for="(item,index) in cateList" :key="item.cat_id">{{item.cat_name}}</view>
			</scroll-view>
			<scroll-view class="scroll-right" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop">
				<!-- 二级分类 -->
				<view class="cate-lv2" v-for="(item,index) in cateList2" :key="index">
					<view class="cate-lv2-title">{{item.cat_name}}</view>
					<!-- 三级分类 -->
					<view class="cate-lv3-list">
						<view class="cate-lv3-item" v-for="(item2,i) in item.children" :key="i" @click="goToGoodsList(item2)">
							<image :src="item2.cat_icon"></image>
							<text>{{item2.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wh: 0,
				cateList: [],
				cateList2: [],
				active: 0,
				scrollTop: 0
			};
		},
		onLoad() {
			this.wh = uni.getSystemInfoSync().windowHeight - 50
			this.getCateList()
		},
		methods: {
			goToGoodsList(item) {
				const cat_id = item.cat_id
				uni.navigateTo({
					url: `/subpkg/goods-list/goods-list?cid=${cat_id}`
				})
			},
			async getCateList() {
				let {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/categories')
				if (meta.status !== 200) return uni.$showMsg()
				this.cateList = message
				this.cateList2 = message[0].children
			},
			activeChange(index) {
				this.active = index
				this.cateList2 = this.cateList[index].children
				this.scrollTop = this.scrollTop ? 0 : .1
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.scroll-left {
			width: 120px;

			.left-scroll-item {
				line-height: 60px;
				text-align: center;
				background-color: #f7f7f7;
				font-size: 12px;

				// 点击状态下
				&.active {
					background-color: #fff;
					position: relative;

					&::before {
						content: '';
						display: block;
						position: absolute;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						top: 50%;
						left: 0;
						transform: translateY(-50%);
					}
				}

			}

		}
		.scroll-right {
			.cate-lv2 {
				.cate-lv2-title {
					text-align: center;
					font-size: 12px;
					font-weight: bold;
					padding: 15px 0;
				}
				.cate-lv3-list {
					display: flex;
					flex-wrap: wrap;
					.cate-lv3-item {
						width: 33.33%;
						display: flex;
						flex-direction: column;
						align-items: center;
						image {
							width: 60px;
							height: 60px;
						}
						text {
							font-size: 12px;
						}
					}	
				}
			}
		}
	}
	
	.search-box {
	  // 设置定位效果为“吸顶”
	  position: sticky;
	  // 吸顶的“位置”
	  top: 0;
	  // 提高层级，防止被轮播图覆盖
	  z-index: 999;
	}
</style>
